package datamining.visualisation;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.File;
import java.util.Properties;

import javax.swing.JMenuBar;

import settings.Settings;

import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JRadioButtonMenuItem;

import org.w3c.dom.events.UIEvent;

/** 
 * 创建于 2012-11-21 下午2:09:37
 * 
 * @author xujinghu (jinghuxu@nudt.edu.cn)
 * 
 * Copyright 2012 xujinghu
 * 
 * Lisence: Apache
 */
public class MainMenu extends JMenuBar implements ActionListener,
PropertyChangeListener {

	private Properties props = null;
	private PropertyChangeSupport mmListeners;
		
	public MainMenu(Properties props) {
		this.props = props;
		mmListeners = new PropertyChangeSupport(this);
		
		
		JMenu Menu_File = new JMenu(props.getProperty("menu_File"));		//文件菜单
		add(Menu_File);
		
		JMenuItem MenuItem_OpenFile = new JMenuItem(props.getProperty("menu_OpenFile"));	//打开文件按钮
		MenuItem_OpenFile.addActionListener(this);
		Menu_File.add(MenuItem_OpenFile);
		
		JMenuItem MenuItem_Exit = new JMenuItem(props.getProperty("menu_Exit"));//	退出按钮
		MenuItem_Exit.addActionListener(this);
		Menu_File.add(MenuItem_Exit);
		
		JMenu menu_settings = new JMenu(props.getProperty("menu_Settings"));		//设置菜单
		add(menu_settings);
		
		JMenu Menu_GraphLayout = new JMenu(props.getProperty("menu_GraphLayout"));	//图像布局按钮
		menu_settings.add(Menu_GraphLayout);
		
		JRadioButtonMenuItem RadioItem_TreeLayout = new JRadioButtonMenuItem(props.getProperty("menu_TreeLayout"));	//树状布局
		Menu_GraphLayout.add(RadioItem_TreeLayout);
		
		JCheckBoxMenuItem CheckItem_HighQuality = new JCheckBoxMenuItem(props.getProperty("menu_HighQuality"));	//清晰画质按钮
		menu_settings.add(CheckItem_HighQuality);
		
		JMenu Menu_Windows = new JMenu(props.getProperty("menu_Windows"));	//窗口菜单
		add(Menu_Windows);
		
		JMenuItem MenuItem_Preferences = new JMenuItem(props.getProperty("menu_Preferences"));	//偏好
		Menu_Windows.add(MenuItem_Preferences);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		final String command = event.getActionCommand();
		
		if (command.equals(props.getProperty("menu_Exit"))) {	//退出系统
			System.exit(0);
		}
		
		if (command.equals(props.getProperty("menu_OpenFile"))) {	//打开文件

			final JFileChooser chooser = new JFileChooser();
			chooser.setDialogType(JFileChooser.OPEN_DIALOG);
			chooser.setMultiSelectionEnabled(false);
			chooser.setDialogTitle(props
					.getProperty("menu_OpenFile"));
			chooser.setCursor(Cursor
					.getPredefinedCursor(Cursor.DEFAULT_CURSOR));

			final String oldPath = "";
			if (oldPath != null) {
				try {
					final File oldDir = new File(oldPath);
					chooser.setCurrentDirectory(oldDir);
				} catch (final Exception e) {
					// again be very quiet
				}
			}
			final int option = chooser.showOpenDialog(new JFrame());
			if (option == JFileChooser.APPROVE_OPTION) {
				mmListeners.firePropertyChange(UIEvents.openFile,
						null, chooser.getSelectedFile());//存储选取的文件路径

			}
			this.setCursor(Cursor
					.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			return;
		}
	}
	
	@Override
	public void addPropertyChangeListener(final PropertyChangeListener l) {
		mmListeners.addPropertyChangeListener(l);
	}
	
	@Override
	public void addPropertyChangeListener(final String string,
			final PropertyChangeListener l) {
		mmListeners.addPropertyChangeListener(string, l);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		// TODO Auto-generated method stub
		
	}

}
